package com.lancoo.znbkxx.uis;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.znbk.znbklibrary.base.BaseLongActivity;
import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.adapter.GuideAdapter;
import com.lancoo.znbkxx.base.AppActivityManager;
import com.lancoo.znbkxx.base.AppSettingsBean;
import com.lancoo.znbkxx.base.BaseActivity;
import com.lancoo.cpbase.authentication.base.AddressOperater;
import com.lancoo.cpbase.authentication.base.ExitBack;
import com.lancoo.cpbase.authentication.base.LoginBack;
import com.lancoo.cpbase.authentication.base.LoginOperate;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Create by TangJun on 2019/2/16
 */
public class GuideActivity extends BaseLongActivity implements ViewPager.OnPageChangeListener{
    private ViewPager vPager;
    private GuideAdapter gAdapter;
    SharedPreferences sp;
    private static  int[] imgs = {R.mipmap.page_one,R.mipmap.page_two, R.mipmap.page_three};
    private ArrayList<ImageView>imageViews;
    private ImageView[] dotViews;//小圆点
    private LoginOperate operate;


//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_guide);
//        vPager = findViewById(R.id.guide_ViewPager);
//        operate = new LoginOperate(this);
//        initImages();
//        initDots();
//        gAdapter = new GuideAdapter(imageViews);
//        vPager.setAdapter(gAdapter);
//        vPager.addOnPageChangeListener(this);
//        AppActivityManager.getInstance().addActivity(this);
//    }

    @Override
    protected int setLayout() {
        return R.layout.activity_guide;
    }

    @Override
    protected void initView() {
        vPager = findViewById(R.id.guide_ViewPager);
        operate = new LoginOperate(this);
        initImages();
        initDots();
        gAdapter = new GuideAdapter(imageViews);
        vPager.setAdapter(gAdapter);
        vPager.addOnPageChangeListener(this);
        AppActivityManager.getInstance().addActivity(this);
    }

    /**
     * 把引导页要显示的图片添加到集合中，以传递给适配器，用来显示图片。
     */
    private void initImages(){
        //设置每一张图片都填充窗口
        ViewPager.LayoutParams mParams = new ViewPager.LayoutParams();
        imageViews = new ArrayList<>();

        for(int i=0; i<imgs.length; i++)
        {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);//设置布局
            iv.setImageResource(imgs[i]);//为ImageView添加图片资源
            iv.setScaleType(ImageView.ScaleType.FIT_XY);//这里也是一个图片的适配
            imageViews.add(iv);
            if (i == imgs.length -1 ){
                //为最后一张图片添加点击事件
                iv.setOnTouchListener(new View.OnTouchListener(){
                    @Override
                    public boolean onTouch(View v, MotionEvent event){
                        int result = operate.getCurrentUserState();
                        if (result == 2) {
                            translate(result);
                        } else {
                            translate(result);
                        }
                        finish();
                        return true;
                    }
                });
            }

        }
    }
    private void translate(int result) {
        switch (result) {
            case 1:
                onLoginSuccessDo(false);
                finish();
                break;
            default:
                goToLoginWidthIdentityJudgment(true);
                break;
        }
    }

    private void onLoginSuccessDo(boolean nessaryDo) {
        AddressOperater addressOperater = new AddressOperater(GuideActivity.this);
        String nowAddress = addressOperater.getBaseAddress();
//        if (CurrentUser.UserType == 0) {
//            gotoNormalEnter();
//        } else {
//            // gotoNonaClass();
//        }
        gotoNormalEnter();
    }
    public void goToLoginWidthIdentityJudgment(final boolean nessaryDo) {
        new LoginOperate(this).goToLoginActivity(this,
                new LoginBack() {
                    @Override
                    public void loginSuccess() {
                        onLoginSuccessDo(nessaryDo);
                    }
                }, new ExitBack() {
                    @Override
                    public void exit() {
                        AppActivityManager.getInstance().exit();
                    }
                }, false);
    }

    private void gotoNormalEnter() {
        sp = getApplication().getSharedPreferences("firstEnter",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isFirst",false);
        editor.commit();
        Intent intent = new Intent();
        intent.setClass(GuideActivity.this, PhoneMainActivity.class);
        startActivity(intent);
        AppSettingsBean.setIsFirstLaunch(false);
        finish();
    }
    private void initDots(){
        LinearLayout layout = findViewById(R.id.dot_Layout);
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(60, 60);
        mParams.setMargins(15, 0, 15,0);//设置小圆点左右之间的间隔
        dotViews = new ImageView[imgs.length];
        //判断小圆点的数量，从0开始，0表示第一个
        for(int i = 0; i < imageViews.size(); i++)
        {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(mParams);
            imageView.setImageResource(R.drawable.dotselector);
            if(i== 0)
            {
                imageView.setSelected(true);//默认启动时，选中第一个小圆点
            }
            else {
                imageView.setSelected(false);
            }
            dotViews[i] = imageView;//得到每个小圆点的引用，用于滑动页面时，（onPageSelected方法中）更改它们的状态。
            layout.addView(imageView);//添加到布局里面显示
        }

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    /**
     * arg0：当前滑动显示页面的索引值，可以根据这个值，来设置相应小圆点的状态。
     */
    @Override
    public void onPageSelected(int arg0) {
        for(int i = 0; i < dotViews.length; i++)
        {
            if(arg0 == i)
            {
                dotViews[i].setSelected(true);
            }
            else {
                dotViews[i].setSelected(false);
            }
        }

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            exitBy2Click();      //调用双击退出函数
        }
        return false;
    }
    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;
    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            System.exit(0);
        }
    }
}


